package com.itheima.exer.homework.hw0928;

import java.util.Comparator;
import java.util.HashSet;

/**
 * 定义学生类Student，属性：姓名、性别、年龄
 * 定义测试类，及main()方法
 * 定义一个存储Student类型的HashSet集合
 * 创建以下三个Student对象
 * 张三,男,20
 * 李四,女,21
 * 张三,男,20
 * 将上述三个对象存储到Set集合中
 * 使用增强for遍历集合，获取每个Student对象，并打印属性值；
 * 请实现集合中不能存储姓名年龄相同的元素
 */
public class HashSetDemo {
    public static void main(String[] args) {
        HashSet<Student> ss = new HashSet<Student>();
        ss.add(new Student("张三","男",20));
        ss.add(new Student("李四","女",21));
        ss.add(new Student("张三","女",20));
        System.out.println(ss);
    }
}
class Student implements Comparable<Student>{
    private String name;
    private String gender;
    private Integer age;

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Student student = (Student) o;

        if (name != null ? !name.equals(student.name) : student.name != null) return false;
        if (gender != null ? !gender.equals(student.gender) : student.gender != null) return false;
        return age != null ? age.equals(student.age) : student.age == null;
    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + (gender != null ? gender.hashCode() : 0);
        result = 31 * result + (age != null ? age.hashCode() : 0);
        return result;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Student() {
    }

    public Student(String name, String gender, Integer age) {
        this.name = name;
        this.gender = gender;
        this.age = age;
    }

    @Override
    public int compareTo(Student o) {

        return 0;
    }
}
